<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Redis\Channel;

/**
 * Class ManyiController
 * @package Pay\Controller
 *
 * 7/9 ajie 熊猫
 */
class XiongmaoController extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );


        switch($this->config['paytype']){
            //支付宝
            case 17:
            case 18:
                $pay_bankcode = 'alipay';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $data = array();
        $data['action'] = "order";
        $data['userid'] = $this->config['mch_id'];
        $data['cusid'] = mt_rand(1000,9999);
        $data['token'] = time()+mt_rand(100000,999999);
        $data['out_trade_no'] = $return['orderid'];
        $data['value_cny'] = number_format($return['amount'],4,'.','');
        $data['pay_type'] = $pay_bankcode;
        $data['ip'] = getIP();

        ksort( $data );
        $str = "";
        foreach ( $data as $key => $value )
        {
            if( $value!="" )
                $str .= $key."=".$value."&";
        }

        $str .=   $this->config['signkey'];

        $data['sign'] = md5( $str );

        $res = curl_json($this->config['gateway'], $data); //json提交
        $info = json_decode($res, true);

        //var_dump('<pre/>',$this->config['gateway'],$data,$str,$res,$info);exit();

        $code = $info['data']['url'];
        if($info['error'] !== 0){
            if($info['info']){
                $this->showmessage($info['info'],['code'=>C("THIRDCODES.".$this->class)]);
            }
            $this->showmessage("获取支付链接失败",['code'=>C("THIRDCODES.".$this->class)]);
        }

        header("Location:{$code}");
    }

    //通知
    public function notifyurl(){
       // $this->debug_callback();
        if(!$this->checkip(getIP(),['103.214.194.22'])){
           // exit("wrong");
        }
       // $p = json_decode(file_get_contents("php://input"), true);

        //{"action":"setPass","coin":"usdt","cusid":"6149","out_trade_no":"sn20190709162839209","token":"20190709042931279528","trade_no":"190709162839_000057_DGGydp","value_cny":"200.00","value_coin":"28.90173410","sign":"1a5cf6a51ec67c4c5d9ab154905d2507"}

        $p = json_decode('{"action":"setPass","coin":"usdt","cusid":"6149","out_trade_no":"sn20190709162839209","token":"20190709042931279528","trade_no":"190709162839_000057_DGGydp","value_cny":"200.00","value_coin":"28.90173410","sign":"1a5cf6a51ec67c4c5d9ab154905d2507"}', true);
        if(empty($p)){
            exit("");
        }

        //获取订单信息
        $orderid = $p['out_trade_no'];
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(
            "pay_orderid_rd"=> htmlspecialchars($p['trade_no'])
        ));

        $this->config = Channel::getInstance()->find(['code'=>$this->class]);

       /* if($p['action'] !== 'setPass'){
            exit("fail");
        }*/

        $sign = $p['sign'];


        $data['cusid'] = $p['cusid'];
        $data['trade_no'] = $p['trade_no'];
        $data['out_trade_no'] = $p['out_trade_no'];
        $data['sn'] = $p['sn'];
        $data['coin'] = $p['coin'];
        $data['value_cny'] = $p['value_cny'];
        $data['value_coin'] = $p['value_coin'];
        $data['token'] = $p['token'];
        $data['action'] = $p['action'];

        ksort( $p );
        $str = "";
        foreach ( $p as $key => $value )
        {
            if( $value!="" )
                $str .= $key."=".$value."&";
        }

        $str .= $this->config['signkey'];
        $sign2 = md5($str);

        var_dump('<pre/>',$sign,$sign2,$p,$this->config['signkey'],$str);exit();

        if($sign !== $sign2){
            echo 'sign_wrong';exit;
        }

        $this->async($find['pay_orderid'],'ok');
    }

    public function callBackUrl(){

        echo '操作成功'; exit;
    }
}